package com.wen.business.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wen.business.mapper.LoginLogMapper;
import com.wen.business.service.LoginLogService;
import com.wen.common.pojo.account.LoginLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *
 * @author liwen <devinlive@163.com>
 * @version 1.0
 * @since 2022-10-31 22:30
 */
@Service
@Slf4j
public class LoginLogImpl implements LoginLogService {

    @Autowired
    private LoginLogMapper loginLogMapper;


    @Override
    public Page<LoginLog> findPage(int pageNum, int pageSize, String beginTime, String endTime, String loginUser) {

        LambdaQueryWrapper<LoginLog> queryWrapper = Wrappers.lambdaQuery();
        if (StrUtil.isNotBlank(loginUser)) {
            queryWrapper.like(LoginLog::getLoginUser, loginUser);
        }
        QueryWrapper<LoginLog> wrapper = new QueryWrapper<>();
        if ("".equals(beginTime) && "".equals(endTime)) {
            return loginLogMapper.selectPage(new Page<LoginLog>(pageNum,pageSize),queryWrapper);
        } else if ("".equals(endTime)) { // 有开始时间
            wrapper.ge("login_time", beginTime).le("login_time", System.currentTimeMillis());
        } else if ("".equals(beginTime)) { // 有结束时间
            wrapper.le("login_time", endTime);
        }else {
            wrapper.ge("login_time", beginTime).le("login_time", endTime);
        }

        return loginLogMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
    }
}
